package com.meitianhui.convenitenbuy.module.mine.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.meitianhui.convenitenbuy.R;
import com.meitianhui.convenitenbuy.util.GlideUtils;

import java.util.List;

/**
 *  Created by amy on 2017/11/16.
 */

public class FadeBackPhotoAdapter extends RecyclerView.Adapter<FadeBackPhotoAdapter.ViewHolder> {


    private OnItemClickListener mListener;
    private List<Uri> mPhotoList;
    private Context mContext;

    public FadeBackPhotoAdapter(Context context, @Nullable List<Uri> data ,OnItemClickListener listener ) {
        this.mContext = context;
        this.mPhotoList = data;
        this.mListener = listener;
    }

    public void setData(List<Uri> photoList){
        mPhotoList = photoList;
        notifyDataSetChanged();
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_fade_back_add_photo, parent, false);
        ViewHolder viewHolder = new ViewHolder(view,mListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (position == mPhotoList.size()){

            holder.mPhoto.setImageResource(R.mipmap.mine_fadeback_add_photo);
            if (position == 5){
                holder.mPhoto.setVisibility(View.INVISIBLE);
            }
        }else {
            //加载其他的图片
            String imgPath = mPhotoList.get(position).toString();
            GlideUtils.setRectImage(mContext,imgPath,R.mipmap.mine_fade_default_picture_,R.mipmap.mine_fade_default_picture_,holder.mPhoto);
        }
    }

    @Override
    public int getItemCount() {
        int size = mPhotoList.size();
        if (size == 5){
            return size;
        }
        return size+1;
    }

    //设置图片点击监听
    public interface OnItemClickListener{
        void onItemClick(int position);
    }

    public class  ViewHolder extends RecyclerView.ViewHolder{

        private ImageView mPhoto;

        public ViewHolder(View itemView, OnItemClickListener listener) {
            super(itemView);
            mPhoto = (ImageView)itemView.findViewById(R.id.iv_item_fade_back_photo);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemClick(getPosition());
                }
            });
        }
    }
}
